package net.zabuchy.helper;

import java.io.Serializable;
import java.util.Map;

import net.zabuchy.iteration.bo.IterationBo;
import net.zabuchy.iteration.entity.Iteration;
import net.zabuchy.user.bo.UserBo;
import net.zabuchy.user.entity.User;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;

public class SessionHelper implements Serializable {
	
	private static final long serialVersionUID = 1L;

	@Autowired
	private UserBo userBo;
	
	@Autowired
	private IterationBo iterationBo;
	
	User user;
	private Map<String, Object> session;
	
	private static final Logger LOGGER = Logger.getLogger(SessionHelper.class.getName());
	
	
	public void setCustomerId(Integer id) {
		
		session = ActionContext.getContext().getSession();
		
		if(session.containsKey(ParamNames.CUSTOMER_ID)) {
			if(!(id == (Integer)session.get(ParamNames.CUSTOMER_ID))) {
				session.remove(ParamNames.CUSTOMER_ID);
				session.put(ParamNames.CUSTOMER_ID, id);
			}
		} else {
			session.put(ParamNames.CUSTOMER_ID, id);
		}
		LOGGER.debug("put CustomerId: " + id);
	}
	
	public Integer getCustomerId() {
		
		session = ActionContext.getContext().getSession();
		Integer customerId = null;
		
		try {
			customerId = (Integer) session.get(ParamNames.CUSTOMER_ID);
		} catch (Exception e) {
			LOGGER.error("problems with geting customerID from session: " + e.getMessage());
		}
		
		if(! (customerId == null)) {
			return customerId;
		} else {
			return ParamValues.INVALID_CUSTOMER_ID;
		}
	}
	
	public User getExecutor() {
		
		session = ActionContext.getContext().getSession();
		User executor = null;
		
		try { 
			executor = (User) session.get(ParamNames.EXECUTOR);
		} catch (Exception e) {
			LOGGER.error("problems with get executor from session: " + e.getMessage());
		}
		
		if(! (executor == null)) {
			return executor;
		} else {
			return null;
		}
	}
	
	public String getExecutorRole() {
		
		session = ActionContext.getContext().getSession();
		String executorRole = null;
		
		try { 
			executorRole = (String) session.get(ParamNames.EXECUTOR_ROLE);
		} catch (Exception e) {
			LOGGER.error("problems with get executorRole from session: " + e.getMessage());
		}
		
		if(! (executorRole == null)) {
			return executorRole;
		} else {
			return null;
		}
	}
	
	public void setExecutor(User user) {
		
		session = ActionContext.getContext().getSession();
		String executorRole = userBo.getUserRole(user);
		
		if(session.containsKey(ParamNames.EXECUTOR)) {
			User userFromsession = (User) session.get(ParamNames.EXECUTOR);
			
			if(userFromsession != null &&
					user.getUserId() == userFromsession.getUserId()) {
				session.remove(ParamNames.EXECUTOR);
				session.remove(ParamNames.EXECUTOR_ROLE);
				
				session.put(ParamNames.EXECUTOR, user);
				session.put(ParamNames.EXECUTOR_ROLE, executorRole);
			}
		} else {
			session.put(ParamNames.EXECUTOR, user);
			session.put(ParamNames.EXECUTOR_ROLE, executorRole);
		}
		LOGGER.debug("put executor: " + user.getUsername());
	}
	
	public void setProjectId(Integer id) {
		
		session = ActionContext.getContext().getSession();
		
		if(session.containsKey(ParamNames.CURRENT_PROJECT_ID)) {
			if(!(id == (Integer)session.get(ParamNames.CURRENT_PROJECT_ID))) {
				session.remove(ParamNames.CURRENT_PROJECT_ID);
				session.put(ParamNames.CURRENT_PROJECT_ID, id);
			}
		} else {
			session.put(ParamNames.CURRENT_PROJECT_ID, id);
		}
		LOGGER.debug("put ProjectId: " + id);
	}
	
	public Integer getProjectId() {
		
		session = ActionContext.getContext().getSession();
		Integer projectId = null;
		
		try {
			projectId = (Integer) session.get(ParamNames.CURRENT_PROJECT_ID);
		} catch (Exception e) {
			LOGGER.error("problems with geting projectId from session: " + e.getMessage());
		}
		
		if(! (projectId == null)) {
			return projectId;
		} else {
			return ParamValues.INVALID_PROJECT_ID;
		}
	}
	
	public void setActualIteration(Iteration iteration) {
		
		if(iteration == null) {
			LOGGER.debug("error");
			return;
		}
		session = ActionContext.getContext().getSession();
		if(session.containsKey(ParamNames.ACTUAL_ITERATION)) {
			Iteration itFromSession = (Iteration) session.get(ParamNames.ACTUAL_ITERATION);
			if(itFromSession != null &&
					itFromSession.getIterationId() == iteration.getIterationId()) {
				session.remove(ParamNames.ACTUAL_ITERATION);
				session.put(ParamNames.ACTUAL_ITERATION, iteration);
			}
		} else {
			session.put(ParamNames.ACTUAL_ITERATION, iteration);
		}
		LOGGER.debug("put actualIteration - iterationNumber: " + iteration.getIterationNumber());
	}
	
	public Iteration getActualIteration() {
		
		session = ActionContext.getContext().getSession();
		Iteration iteration = null;
		try { 
			iteration = (Iteration) session.get(ParamNames.ACTUAL_ITERATION);
		} catch (Exception e) {
			LOGGER.error("problems with get actual iteration from session:" + e.getMessage());
		}
		if(! (iteration == null)) {
			return iteration;
		} else {
			return null;
		}
	}
	
	

	public UserBo getUserBo() {
		return userBo;
	}

	public void setUserBo(UserBo userBo) {
		this.userBo = userBo;
	}

	public void setIterationBo(IterationBo iterationBo) {
		this.iterationBo = iterationBo;
	}

	public IterationBo getIterationBo() {
		return iterationBo;
	}
	
}
